﻿package views {
	import at.geoathome.text.TextFieldGenerator;
	import at.geoathome.ui.buttons.Button;
	import at.geoathome.ui.buttons.TextButton;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;	

	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class StartScreenView extends Sprite
	{
		private var _drawn:Boolean;
		private var _game:Game;
		// visual
		private var _game_title:Sprite;
		private var _player1_title:Sprite;
		private var _player1_color_box:Sprite;
		private var _player1_name_txt:TextField;
		private var _player2_title:Sprite;
		private var _player2_color_box:Sprite;
		private var _player2_name_txt:TextField;
		private var _start_btn:Button;
		private var _settings_btn:TextButton;
		private var _how_to_play_title:Sprite;
		
		public function StartScreenView(game:Game) 
		{
			_game = game;
			_drawn = false;
			hide();
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			if (visible)
				show();
		}
		
		private function extractAssets():void
		{
			_game_title			= new (_game.asset_loader.getDefinitionOf("TowersLogo"))();
			_player1_title		= new (_game.asset_loader.getDefinitionOf("Player1NameInput"))();
			_player1_name_txt	= TextFieldGenerator._getDefaultTextField("Player I", 100, 26, 16, true, true);
			_player2_title		= new (_game.asset_loader.getDefinitionOf("Player2NameInput"))();
			_player2_name_txt	= TextFieldGenerator._getDefaultTextField("Player II", 100, 26, 16, true, true);
			_start_btn			= new Button("StartBtnBackground");
			_settings_btn		= new TextButton("DefaultButtonBg");
			_how_to_play_title	= new (_game.asset_loader.getDefinitionOf("HowToPlay"))();
		}
		
		private function draw():void 
		{
			extractAssets();
			
			// title
			_game_title.x = (stage.stageWidth - _game_title.width) / 2;
			_game_title.y = 20;
			addChild(_game_title);
			
			// player 1 input
			_player1_title.x = stage.stageWidth / 2 - 40 - _player1_name_txt.width - 5 - _player1_title.width;
			_player1_title.y = 140;
			addChild(_player1_title);
			
			_player1_name_txt.x = stage.stageWidth / 2 - 40 - _player1_name_txt.width;
			_player1_name_txt.y = _player1_title.y + 8;
			_player1_name_txt.type = TextFieldType.INPUT;
			_player1_name_txt.border = false;
			_player1_name_txt.background = true;
			_player1_name_txt.backgroundColor = 0xeeeeee;
			addChild(_player1_name_txt);
			
			// player 2 input
			_player2_title.x = stage.stageWidth / 2 + 40;
			_player2_title.y = _player1_title.y;
			addChild(_player2_title);
						
			_player2_name_txt.x = stage.stageWidth / 2 + 40 + _player2_title.width + 5;
			_player2_name_txt.y = _player2_title.y + 8;
			_player2_name_txt.type = TextFieldType.INPUT;
			_player2_name_txt.border = false;
			_player2_name_txt.background = true;
			_player2_name_txt.backgroundColor = 0xeeeeee;
			addChild(_player2_name_txt);
			
			// start btn
			_start_btn.x = (stage.stageWidth - _start_btn.width)/2 ;
			_start_btn.y = 220;
			_start_btn.addEventListener(MouseEvent.MOUSE_DOWN, onStartPressed);
			addChild(_start_btn);
			
			_settings_btn.width = 100;
			_settings_btn.height = 35;
			_settings_btn.x = _start_btn.x + _start_btn.width + 80;
			_settings_btn.y = 220;
			_settings_btn.addLabel("Settings", 0x000000, 14);
			_settings_btn.addEventListener(MouseEvent.MOUSE_DOWN, onSettingsPressed);
			addChild(_settings_btn);
			
			_how_to_play_title.x = 30;
			_how_to_play_title.y = 250;
			addChild(_how_to_play_title);
			
			_drawn = true;
		}
		
		private function onStartPressed(e:MouseEvent):void 
		{
			_game.setPlayerData(_player1_name_txt.text, _player2_name_txt.text);
			_game.changeStateToGameSelection();
			hide();
		}
		private function onSettingsPressed(e:MouseEvent):void 
		{
			_game.changeStateToSettingsScreen();
			hide();
		}
		
		public function show():void 
		{
			if (!_drawn && stage)
				draw();
				
			visible = true;
		}
		
		public function hide():void 
		{
			visible = false;
		}
	}
	
}